//
//  MeVC.m
//  HZAPP
//
//  Created by huazi on 15/9/4.
//  Copyright (c) 2015年 huazi. All rights reserved.
//

#import "MeVC.h"

#import "PersonLoginedCell.h"
#import "PersonNoLoginCell.h"
#import "CommonTableViewCell.h"
//
//#import "AddressManagerVC.h"
//#import "FeedbackVC.h"
//#import "SettingVC.h"
//#import "AboutVC.h"
@interface MeVC ()
@property (strong, nonatomic)NSArray *arrDataSource;
@end

@implementation MeVC
- (void)setData {
    self.titleForNav =@"我";
    self.arrDataSource =@[@[@""],@[@{@"image":@"",@"title":@"常用地址管理"},@{@"image":@"",@"title":@"意见反馈"}],@[@{@"image":@"",@"title":@"设置"},@{@"image":@"",@"title":@"关于"}]];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setSubviews];
    // Do any additional setup after loading the view.
}
- (void)setSubviews {
    self.table =[[UITableView alloc] initWithFrame:CGRectMake(0, 0, DeviceWidth, DeviceHeight-64.0-49.0)];
    self.table.delegate =self;
    self.table.dataSource =self;
    self.table.showsVerticalScrollIndicator =NO;
    self.table.separatorStyle =UITableViewCellSeparatorStyleNone;
    [self.table registerClass:[PersonLoginedCell class] forCellReuseIdentifier:[PersonLoginedCell cellIdentifier]];
    [self.table registerClass:[PersonNoLoginCell class] forCellReuseIdentifier:[PersonNoLoginCell cellIdentifier]];
    [self.table registerClass:[CommonTableViewCell class] forCellReuseIdentifier:[CommonTableViewCell cellIdentifier]];
    [self.view addSubview:self.table];
    
    UIView *viewHeader =[[UIView alloc] initWithFrame:CGRectMake(0, 0, DeviceWidth, 10.0)];
    viewHeader.backgroundColor =[UIColor colorWithHexString:kcolorViewBJ];
    self.table.tableHeaderView =viewHeader;
    self.table.backgroundColor =[UIColor colorWithHexString:kcolorViewBJ];
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:YES];
    Person *_person =[Person sharePerson];
    if (_person.isLogin) {
        [self loadUserDataFormServer];
    }
    [self.table reloadData];
}
- (void)loadUserDataFormServer {
    Person *_person =[Person sharePerson];
    NSDictionary *dicParams =[NSDictionary dictionaryWithObjectsAndKeys:_person.userId,@"userId",nil];
    HZMRequest *request =[[HZMRequest alloc] init];
    request.requsetId =kUserCheckMyMessage;
    request.requestParamDic =dicParams;
    request.callBackDelegate =self;
    [[HZMAPImanager shareMAPImanager] postWithWSRequest:request];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark-table
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.arrDataSource.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [((NSArray *)[self.arrDataSource objectAtIndex:section]) count];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section ==0)
    {
        return 80.0f;
    }
    return 44.0f;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section ==0)
    {
        Person *_person =[Person sharePerson];
        if (_person.isLogin) {
            PersonLoginedCell *cell =[tableView dequeueReusableCellWithIdentifier:[PersonLoginedCell cellIdentifier]];
            [cell setDataCell:nil];
            return cell;
        }
        PersonNoLoginCell *cell =[tableView dequeueReusableCellWithIdentifier:[PersonNoLoginCell cellIdentifier]];
        [cell setDataCell:nil];
        return cell;
    }
    else
    {
        CommonTableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:[CommonTableViewCell cellIdentifier]];
        id model =[[self.arrDataSource objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
        [cell setDataCell:model];
        cell.accessoryType =UITableViewCellAccessoryDisclosureIndicator;
        return cell;
    }
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.section ==0) {
        
    }
//    if (indexPath.section ==1) {
//        if (indexPath.row ==0) {
//            if ([self isLogined]) {
//                AddressManagerVC *_addressManager =[[AddressManagerVC alloc] init];
//                [self HZPushTo:_addressManager];
//            }
//            return;
//        }
//        FeedbackVC *_feedBack =[[FeedbackVC alloc] init];
//        [self HZPushTo:_feedBack];
//    }
//    if (indexPath.section ==2) {
//        if (indexPath.row ==0) {
//            if ([self isLogined]) {
//                SettingVC *_setting =[[SettingVC alloc] init];
//                [self HZPushTo:_setting];
//            }
//            return;
//        }
//        AboutVC *_about =[[AboutVC alloc] init];
//        [self HZPushTo:_about];
//    }
//    if (indexPath.section ==0)
//    {
//        PersonMesageVC *_personMessageVC =[self.storyboard instantiateViewControllerWithIdentifier:@"PersonMesageVC"];
//        [self.navigationController pushViewController:_personMessageVC animated:YES];
//    }
//    else if(indexPath.section ==1)
//    {
//        Person *_person =[Person sharePerson];
//        if ([_person.loginType isEqualToString:kLoginTypedoctor]) {
//            if (indexPath.row ==0) {
//                MyBooks *_mybook =[[MyBooks alloc] init];
//                [self.navigationController pushViewController:_mybook animated:YES];
//            }
//            if (indexPath.row ==1) {
//                MyAttentionVC *_myAttention =[[MyAttentionVC alloc] init];
//                [self.navigationController pushViewController:_myAttention animated:YES];
//            }
//            if (indexPath.row ==2) {
//                MyDisTrends *_myDisTrend =[[MyDisTrends alloc] init];
//                [self.navigationController pushViewController:_myDisTrend animated:YES];
//            }
//            
//        }
//        else {
//            if (indexPath.row ==0) {
//                MyTicketsVC *_myTicket =[[MyTicketsVC alloc] init];
//                [self.navigationController pushViewController:_myTicket animated:YES];
//            }
//            if (indexPath.row ==1) {
//                MyBooks *_mybook =[[MyBooks alloc] init];
//                [self.navigationController pushViewController:_mybook animated:YES];
//            }
//            
//        }
//    }
//    else if(indexPath.section ==2)
//    {
//        Person *_person =[Person sharePerson];
//        if ([_person.loginType isEqualToString:kLoginTypedoctor]) {
//            AboutVC *_about =[self.storyboard instantiateViewControllerWithIdentifier:@"AboutVC"];
//            [self.navigationController pushViewController:_about animated:YES];
//            
//        }
//        else {
//            if (indexPath.row ==0) {
//                MyAttentionVC *_myAttention =[[MyAttentionVC alloc] init];
//                [self.navigationController pushViewController:_myAttention animated:YES];
//            }
//            if (indexPath.row ==1) {
//                MyDisTrends *_myDisTrend =[[MyDisTrends alloc] init];
//                [self.navigationController pushViewController:_myDisTrend animated:YES];
//            }
//            if (indexPath.row ==2) {
//                MyAdvisorysListVC *_myAdvisorys =[[MyAdvisorysListVC alloc] init];
//                [self.navigationController pushViewController:_myAdvisorys animated:YES];
//            }
//        }
//    }
//    else
//    {
//        if (indexPath.row ==0)
//        {
//            DLog(@"关于11");
//            AboutVC *_about =[self.storyboard instantiateViewControllerWithIdentifier:@"AboutVC"];
//            [self.navigationController pushViewController:_about animated:YES];
//            
//        }
//        else
//        {
//            DLog(@"关于");
//        }
//    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 10.0f;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *viewSectionFooter =[[UIView alloc] initWithFrame:CGRectMake(0, 0, DeviceWidth, 10.0)];
    viewSectionFooter.backgroundColor =[UIColor colorWithHexString:kcolorViewBJ];
    return viewSectionFooter;
}
#pragma mark--HZMAPIManagerDelegate
- (void)transactionFinished:(HZMResponse *)response
{
    [MBProgressHUD hideHUDForView:self.view animated:YES];
    
    if ([response.requestId isEqualToString:kUserCheckMyMessage])
    {
        Person *_person =[Person sharePerson];
        [_person initDataWithDic:response.responseData];
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:kLogined];
        [[NSUserDefaults standardUserDefaults] setObject:response.responseData forKey:kUserDic];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
}
- (void)transactionFailed:(HZMResponse *)response
{
    [MBProgressHUD hideHUDForView:self.view animated:YES];
    [self.view makeToast:response.responseMsg duration:1.0 position:@"center"];
}

@end
